//
// Created by tang on 23-4-1.
//

#ifndef UI_ITEM_HPP
#define UI_ITEM_HPP

#include <type_traits>
#include "content/view_base.hpp"
#include <xml/pugixml.hpp>

using pugi::xml_node;

class ui_style;
class ui_options;

class ui_item : public ui_view {
public:
    explicit ui_item(ui_item *parent = nullptr);

    void draw() override;

    void init_by_xml_node(const xml_node &node);

    static ui_item* create_ui(const pugi::xml_node &node, ui_item* parent = nullptr);

    static ui_item *create_ui(const u8 *data, size_t length, ui_item *parent = nullptr);

protected:
    virtual void before_draw();
    virtual void after_draw();
    virtual void init_content(const xml_node &node) = 0;
    virtual void draw_self() = 0;
    virtual void draw_children();

    ui_item       *parent_{};
    ui_style      *style_{};
    ui_options    *options_{};
};

template <typename T>
concept is_ui_type = requires(T) {
    std::is_base_of_v<ui_item, T> && std::is_constructible_v<T, ui_item*>;
};

template <typename T> requires is_ui_type<T>
PI_INLINE ui_item* ui_creator(ui_item* parent) { return new T{ parent }; }

#endif // UI_ITEM_HPP
